package dk.sdu.flexibus;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import dk.sdu.flexibus.domain.Transport;
import dk.sdu.flexibus.persistence.Persister;
import dk.sdu.flexibus.persistence.TransportFactory;

public class ShowAllTransportsActivity extends Activity{

	ArrayAdapter<Transport> transportsAdapter;

	private Spinner transportSpinner;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_all_transports);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		List<Integer> transportIds = TransportFactory.findTransportIdsByState(getPreferences(), new int[]{Transport.STATE_OFFERED});
		List<Transport> transports = new ArrayList<Transport>();
		for(int id : transportIds){
			transports.add(TransportFactory.readTransport(getPreferences(), id));
		}
		transportsAdapter = new ArrayAdapter<Transport>(this,
				android.R.layout.simple_spinner_item, transports);
		transportsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		transportSpinner = (Spinner) findViewById(R.id.allTransportsSpinner);
		transportSpinner.setAdapter(transportsAdapter);
		transportsAdapter.notifyDataSetChanged();
	}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_show_all_transports, menu);
		return true;
	}
	
	public void selectTransport(View view){
		Transport t = (Transport) transportSpinner.getSelectedItem();
		Intent intent = new Intent(this, ShowTransportActivity.class);
		intent.putExtra(ShowTransportActivity.TRANSPORT_ID, t.getId());
		startActivity(intent);
	}
	
	private SharedPreferences getPreferences() {
		return getSharedPreferences(Persister.PREFS_FILE_NAME,
				MODE_PRIVATE);
	}
}
